#include <stdio.h>
#include "metodos.h"
#include "math.h"

int factorial(int a){
    
    int res;
    if (a == 0){
          
          res = 1;
          
    }else{
          res = a;
          int b = a-1;
          while(b > 0){
                         
               res = res * b;
               b--;
                         
          }    
                
    }
    return res;
    }

float arctan(float x, int n){
      
      float numerador;
      float denominador;
      float cociente;
      float res = 0;
      
      if(n != 0){
           int i = 0;
           while(i < n){
                                 
              if ( (i % 2) == 0){
                 numerador =  pow(x,(2*i+1));
              }else{
                 numerador =  (-1)* pow(x,(2*i+1));      
              }      
              denominador = 2*i+1;
              cociente = numerador/denominador;
              res = res + cociente;
              i++;   
           }
      
      
      }
      return res;
}

double arctanDouble(double x, int n){
      
      double numerador;
      double denominador;
      double cociente;
      double res = 0;
      
      if(n != 0){
           int i = 0;
           while(i < n){
              
              if ( (i % 2) == 0){
                 numerador =  pow(x,(2*i+1));
              }else{
                 numerador =  (-1)* pow(x,(2*i+1));      
              }      
              denominador = 2*i+1;
              cociente = numerador/denominador;
              res = res + cociente;
              i++;   
           }
      
      
      }
      return res;
}


float SerieDeGregory(int n){
    float res = arctan(1,n);
    return (res * 4);   
}

double SerieDeGregoryDouble(int n){
    double res = arctanDouble(1,n);
    return (res * 4);   
}

float Machin(int n, int m){
      
      // n representa a la cantidad de terminos a sumar del arctg(1/5)
      // m representa a la cantidad de terminos a sumar del arctg(1/239)
 
    
      float num = 1;
      float dem1 = 5;
      float dem2 = 239;
      
      float res = 4* arctan((num/dem1), n) - arctan((num/dem2), m);
      return (4*res);
      
      }
double MachinDouble(int n, int m){
      
      // n representa a la cantidad de terminos a sumar del arctg(1/5)
      // m representa a la cantidad de terminos a sumar del arctg(1/239)
      
      double num = 1;
      double dem1 = 5;
      double dem2 = 239;
      
      double res = 4* arctan((num/dem1), n) - arctan((num/dem2), m);
      return (4*res);
      
      }

float Ramanujan(int n){
      
      float aux ;
      float aux2; 
      float aux3;
      float raiz;
      float num;
      float dem;
      float coc;
      float res;
      
      if (n ==0){
            
           res = 0; 
            
      }else{
            
            int i = 0;
            while (i < n){
                 
                 aux = factorial(4*i);
                 aux2 = factorial(i); 
                 num = aux * (1103+ (26390*i));
                 dem = pow(aux2, 4)*pow(396, (4*i));
                 coc = num/dem;
                 res = res + coc; 
                 i++; 
                  }
            raiz = sqrt(8);
            aux3 = raiz/9801;
            res = res * aux3;
            res = 1/res;
                  
      }  
      
      return res;      
      }

double RamanujanDouble(int n){
      
      double aux ;
      double aux2; 
      double aux3;
      double raiz;
      double num;
      double dem;
      double coc;
      double res;
      
      if (n ==0){
            
           res = 0; 
            
      }else{
            
            int i = 0;
            while (i < n){
                 
                 aux = factorial(4*i);
                 aux2 = factorial(i); 
                 num = aux * (1103+ (26390*i));
                 dem = pow(aux2, 4)*pow(396, (4*i));
                 coc = num/dem;
                 res = res + coc; 
                 i++; 
                  }
            raiz = sqrt(8);
            aux3 = raiz/9801;
            res = res * aux3;
            res = 1/res;
                  
      }  
      
      return res;      
      }
